package com.zdsoft.blp.zongbuju.wode.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.fanhui.FanHuiView;
import com.zdsoft.blp.gongyong.net.BaseActivity;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;
import com.zdsoft.blp.gongyong.xlistview.XListView;
import com.zdsoft.blp.zongbuju.shouye.taoshihui.adapter.ListViewAdapter;
import com.zdsoft.blp.zongbuju.viewpage.CustomViewPager;
import com.zdsoft.blp.zongbuju.viewpage.adapter.VpFmAdapter;
import com.zdsoft.blp.zongbuju.viewpage.fragMent.FenXiangFragment;
import com.zdsoft.blp.zongbuju.viewpage.fragMent.MyFragment;
import com.zdsoft.blp.zongbuju.viewpage.fragMent.ShouYeFragment;
import com.zdsoft.blp.zongbuju.wode.adapter.OrderAdapter;
import com.zdsoft.blp.zongbuju.wode.entity.Order;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/9/1.
 */
public class WoDeDingDanActivity extends BaseActivity implements View.OnClickListener,NetCallback,XListView.IXListViewListener{
    private XListView xlv;
    private List<Order> list = new ArrayList<>();
    private TextView tv_lqb, tv_lwfk, tv_lwpj, tv_lywc;
    private TextView tv_quanbu, tv_weifukuan, tv_weipingjia, tv_yiwancheng;
    private int yellow, black;
    private FanHuiView fh;
    private OrderAdapter adapter;
    private static  final  int HUOQUDINGDAN =1;
    private Integer pageNo = 0;
    private String orderState ="";
    @Override
    protected void init() {
        setContentView(R.layout.wodedingdan_wode);
        yellow = getResources().getColor( R.color.yellow);
        black =  getResources().getColor( R.color.black);
        xlv = find(R.id.xlv);
        fh = find(R.id.fh);
        fh.getTv_title().setText("我的订单");

        tv_quanbu = find(R.id.tv_quanbu);
        tv_weifukuan = find(R.id.tv_weifukuan);
        tv_weipingjia = find(R.id.tv_weipingjia);
        tv_yiwancheng = find(R.id.tv_yiwancheng);

        tv_lqb = find(R.id.tv_lqb);
        tv_lwfk = find(R.id.tv_lwfk);
        tv_lwpj = find(R.id.tv_lwpj);
        tv_lywc = find(R.id.tv_lywc);

        tv_quanbu.setTextColor(yellow);
        tv_lqb.setVisibility(View.VISIBLE);
        tv_lwfk.setVisibility(View.INVISIBLE);
        tv_lwpj.setVisibility(View.INVISIBLE);
        tv_lywc.setVisibility(View.INVISIBLE);

        getOrderData();
        xlv.setPullLoadEnable(true);
        xlv.setPullRefreshEnable(true);
        xlv.setXListViewListener(this);

        tv_quanbu.setOnClickListener(this);
        tv_weifukuan.setOnClickListener(this);
        tv_weipingjia.setOnClickListener(this);
        tv_yiwancheng.setOnClickListener(this);
        xlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(WoDeDingDanActivity.this, WoDeDingDanXiangQingActivity.class);
                intent.putExtra("orderId", list.get(position-1).getId());
                startActivity(intent);
            }
        });
    }

    @Override
    public void onClick(View v) {
        clear();
        switch (v.getId()) {
            case R.id.tv_quanbu:
                tv_quanbu.setTextColor(yellow);
                tv_lqb.setVisibility(View.VISIBLE);
                orderState ="";
                break;
            case R.id.tv_weifukuan:
                tv_weifukuan.setTextColor(yellow);
                tv_lwfk.setVisibility(View.VISIBLE);
                orderState ="submit_order";
                break;
            case R.id.tv_weipingjia:
                tv_weipingjia.setTextColor(yellow);
                tv_lwpj.setVisibility(View.VISIBLE);
                orderState ="paid";
                break;
            case R.id.tv_yiwancheng:
                tv_yiwancheng.setTextColor(yellow);
                tv_lywc.setVisibility(View.VISIBLE);
                orderState ="evaluation";
                break;
        }
        list = new ArrayList<>();
        pageNo =0;
        getOrderData();
    }


    private void clear() {
        tv_quanbu.setTextColor(black);
        tv_weifukuan.setTextColor(black);
        tv_weipingjia.setTextColor(black);
        tv_yiwancheng.setTextColor(black);

        tv_lqb.setVisibility(View.INVISIBLE);
        tv_lwfk.setVisibility(View.INVISIBLE);
        tv_lwpj.setVisibility(View.INVISIBLE);
        tv_lywc.setVisibility(View.INVISIBLE);
    }

    private void getOrderData() {
        SharedPreferences sp = WoDeDingDanActivity.this.getSharedPreferences("info", Context.MODE_PRIVATE);
        String userId = sp.getString("userId", "");
        Net net = new Net();
        net.url = API.GOODSORDER_URL;
        net.nameSpace = API.GOODSORDER_NAMESPACE;
        net.methodName = API.MYORDERLIST;
        Map<String, Object> map = new HashMap<>();
        map.put("pageNo", pageNo);
        map.put("pageSize", 10);
        map.put("userId", userId);
        map.put("merchantUserId", "");
        map.put("createOrderTimeStart", "");
        map.put("createOrderTimeEnd", "");
        map.put("orderState", orderState);
        map.put("orderNo", "");
        map.put("curTime", new Date().getTime() + "");
        net.parms = map;
        net.code = HUOQUDINGDAN;
        getServer(net, WoDeDingDanActivity.this);
    }

    @Override
    public void onResponse(boolean flag, int code, String res) {
        if(flag&&code==HUOQUDINGDAN){
            if(res!=null&&!"".equals(res)){
                try {
                    JSONObject object = new JSONObject(res);
                    JSONArray array = object.getJSONArray("rows");
                    for(int i = 0;i<array.length();i++){
                        JSONObject orderObject = array.getJSONObject(i);
                        list.add(new Order(orderObject.getString("createOrderTime"),orderObject.getBoolean("isPayment"),orderObject.getString("orderState"),
                                orderObject.getString("merchantGoodsId"),orderObject.getString("orderType"),orderObject.getBoolean("canEvaluation"),
                                orderObject.getBoolean("canPay"),orderObject.getString("orderStateCode"),orderObject.getInt("goodsCount"),
                                orderObject.getBoolean("isOver"),orderObject.getString("endOrderTime"),orderObject.getString("isEvaluation"),
                                orderObject.getString("id"),orderObject.getString("picPath"),orderObject.getString("orderNo"),
                                orderObject.getString("userId"),orderObject.getDouble("money"),orderObject.getString("merchantUserId"),
                                orderObject.getString("payWay"),orderObject.getString("goodsName"),orderObject.getBoolean("canDel")));
                    }
                    if (pageNo == 0) {
                        adapter = new OrderAdapter(WoDeDingDanActivity.this,list);
                        xlv.setAdapter(adapter);
                        pageNo++;
                        xlv.stopRefresh();
                    } else {
                        adapter.setList(list);
                        adapter.notifyDataSetChanged();
                        xlv.stopLoadMore();
                    }
                    pageNo++;
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void onRefresh() {
        list = new ArrayList<>();
        pageNo =0;
        getOrderData();
    }

    @Override
    public void onLoadMore() {
        getOrderData();
    }
}
